{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python Exercise -Version Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- This is the exercise based on the forked repository on GitHub\n",
    "-  Help to improve it if you want, just add the solution under the question\n",
    "- Each notebook only contains 10 questions which is convenient for everyone to locate each question to modify, you can add your own name under the author's list below, let's contribute together !\n",
    "- To visit my personal site: [Click Here](http://yaozeliang.com/resume)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 81\n",
    ">***By using list comprehension, please write a program to print the list after removing numbers which are divisible by 5 and 7 in [12,24,35,70,88,120,155].***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "li = [12,24,35,70,88,120,155]\n",
    "li = [x for x in li if x % 35!=0]\n",
    "print(li)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 82\n",
    ">***By using list comprehension, please write a program to print the list after removing the 0th, 2nd, 4th,6th numbers in [12,24,35,70,88,120,155].***\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "li = [12,24,35,70,88,120,155]\n",
    "li = [ li[x] for x in range(len(li)) if x%2!=0]\n",
    "li"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "li=[12,24,35,70,88,120,155]\n",
    "li=[li[i] for i,x in enumerate(li) if i%2!=0]\n",
    "li"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 83\n",
    "\n",
    ">***By using list comprehension, please write a program to print the list after removing the 2nd - 4th numbers in [12,24,35,70,88,120,155].***\n",
    "\n",
    "\n",
    "> ***Use list comprehension to delete a bunch of element from a list.\n",
    "Use enumerate() to get (index, value) tuple.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[12, 88, 120, 155]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "li=[12,24,35,70,88,120,155]\n",
    "newli=[]\n",
    "for index,value in enumerate(li):\n",
    "    if index not in (1,2,3):\n",
    "        newli.append(value)\n",
    "newli"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 84\n",
    "\n",
    "> ***By using list comprehension, please write a program generate a 3\\*5\\*8 3D array whose each element is 0.***\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[[0, 0, 0, 0, 0, 0, 0, 0],\n",
       "  [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "  [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "  [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "  [0, 0, 0, 0, 0, 0, 0, 0]],\n",
       " [[0, 0, 0, 0, 0, 0, 0, 0],\n",
       "  [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "  [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "  [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "  [0, 0, 0, 0, 0, 0, 0, 0]],\n",
       " [[0, 0, 0, 0, 0, 0, 0, 0],\n",
       "  [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "  [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "  [0, 0, 0, 0, 0, 0, 0, 0],\n",
       "  [0, 0, 0, 0, 0, 0, 0, 0]]]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "value=[[[0 for i in range(8)] for i in range(5)] for i in range(3)]\n",
    "value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 85\n",
    ">***By using list comprehension, please write a program to print the list after removing the 0th,4th,5th numbers in [12,24,35,70,88,120,155].***\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[24, 35, 70, 155]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "li=[12,24,35,70,88,120,155]\n",
    "li=[li[i] for i,value in enumerate(li) if i not in (0,4,5)]\n",
    "li"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 86\n",
    ">***By using list comprehension, please write a program to print the list after removing the value 24 in [12,24,35,24,88,120,155].***\n",
    "\n",
    "----------------------\n",
    "### Hints \n",
    "> ***Use list's remove method to delete a value.***\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "li = [12,24,35,24,88,120,155]\n",
    "li.remove(24)  # this will remove only the first occurrence of 24\n",
    "print(li)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "li = [12,24,35,24,88,120,155]\n",
    "li = [x for x in li if x!=24]  # Delete all values in list if element equals 24\n",
    "li"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 87\n",
    "\n",
    ">***With two given lists [1,3,6,78,35,55] and [12,24,35,24,88,120,155], write a program to make a list whose elements are intersection of the above given lists.***\n",
    "\n",
    "----------------------\n",
    "### Hints \n",
    ">***Use set() and \"&=\" to do set intersection operation.***\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{35}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "li1=[1,3,6,78,35,55]\n",
    "li2=[12,24,35,24,88,120,155]\n",
    "result=set(li1) & set(li2)\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Solution 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 88\n",
    "\n",
    ">***With a given list [12,24,35,24,88,120,155,88,120,155], write a program to print this list after removing all duplicate values with original order reserved.***\n",
    "\n",
    "----------------------\n",
    "### Hints \n",
    "> ***Use set() to store a number of values without duplicate.***|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[35, 88, 12, 24, 155, 120]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "li=[12,24,35,24,120,155,88,120,155]\n",
    "li=list(set(li))\n",
    "li"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 89\n",
    "\n",
    ">***Define a class Person and its two child classes: Male and Female. All classes have a method \"getGender\" which can print \"Male\" for Male class and \"Female\" for Female class.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Person(object):\n",
    "    def getGender( self ):\n",
    "        return \"Unknown\"\n",
    "\n",
    "class Male( Person ):\n",
    "    def getGender( self ):\n",
    "        return \"Male\"\n",
    "\n",
    "class Female( Person ):\n",
    "    def getGender( self ):\n",
    "        return \"Female\"\n",
    "\n",
    "aMale = Male()\n",
    "aFemale= Female()\n",
    "print(aMale.getGender())\n",
    "print (aFemale.getGender())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import random\n",
    "print(random.random()*100-5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question 90\n",
    "\n",
    ">***Please write a program which count and print the numbers of each character in a string input by console.***\n",
    "\n",
    ">***Example:\n",
    "If the following string is given as input to the program:***\n",
    "```\n",
    "abcdefgabc\n",
    "```\n",
    ">***Then, the output of the program should be:***\n",
    "```\n",
    "a,2\n",
    "c,2\n",
    "b,2\n",
    "e,1\n",
    "d,1\n",
    "g,1\n",
    "f,1\n",
    "```\n",
    "### Hints \n",
    "> ***Use dict to store key/value pairs.\n",
    "Use dict.get() method to lookup a key with default value.***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 2, 'b': 2, 'c': 2, 'd': 1, 'e': 1, 'f': 1, 'g': 1}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s=\"abcdefgabc\"\n",
    "res={chr:s.count(chr) for chr in s}\n",
    "res"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
